﻿function getCurrentMapTypeId(){
    return map.getMapTypeId();
}

function resetMap(){
    var currentZoom = map.getZoom();
    initialize();
    map.setZoom(currentZoom);
}

function zoomIn(){
    var currentZoom = map.getZoom();
    currentZoom ++;
    map.setZoom(currentZoom);
}

function zoomOut(){
    var currentZoom = map.getZoom();
    currentZoom --;
    map.setZoom(currentZoom);
}

function setMyLocation(lat, lng, isCenter){
    var myLatLng = new google.maps.LatLng(lat, lng);
    if (myLocationMarker){
        myLocationMarker.setMap(null);
    }		
    myLocationMarker = new google.maps.Marker({
        position: myLatLng,	
        icon: imageMyLocation,	
        map : map
    });	
    if (isCenter == true){
    	map.setCenter(myLatLng);
    }
}

function setMarker(lat, lng, m_title, center){
    var myLatLng = new google.maps.LatLng(lat, lng);
    var maker = new google.maps.Marker({
        position: myLatLng,
        map : map,
        title: m_title
    });
    markersArray.push(marker);
    if (center == true){
   		map.setCenter(myLatLng);
   	}
}

function setMarker(lat, lng, m_title, icon, center){
	var location = new google.maps.LatLng(lat,lng); 
    marker=new google.maps.Marker({
        position:location,
        map:map,
        title: m_title,
        icon:icon
    });      
    markersArray.push(marker); 
    if (center == true){ 
		map.setCenter(location);
	}
}


function drawDirection(url){
    if (ctaLayer){
        ctaLayer.setMap(null);
    }
    ctaLayer = new google.maps.KmlLayer(url);
    ctaLayer.setMap(map);
}

function addMarker(location) {  
    var marker = new google.maps.Marker({
        position: location,
        map: map
    });
    markersArray.push(marker);    
    m_lat=location.lat();
    m_lng=location.lng();   
}
    
function danhDauViTriTraffic(lat, lng,image)
{
    var location=new google.maps.LatLng(lat,lng); 
    marker=new google.maps.Marker({
        position:location,
        map:map,
        icon:image
    });      
    markersArray.push(marker);       
}
function getMyLat()
{
    var result=m_lat;
    m_lat=0;
    return result;
}
function getMyLng()
{
    var result=m_lng;
    m_lng=0;
    return result;
} 
  
// Deletes all markers in the array by removing references to them
function deleteOverlays() {
    if (markersArray) {
        for (i in markersArray) {
            markersArray[i].setMap(null);
        }
        markersArray.length = 0;
    }
}  
function them(lat , lng) {
    var latlng = new google.maps.LatLng(lat,lng);  
    var marker = new google.maps.Marker({
        position: latlng,
        map: map
    });
    markersArray.push(marker);    
    map.setCenter(latlng);
}  

function addStringAddress(m_address){
    m_listString.push(m_address);   
}		

function removeAllStringAddress(){
    m_listString.length = 0;
}

function drawMultiPointDirection(){
    var start = m_listString[0];
    
    var waypts = [];
    
    for (var i = 1; i < m_listString.length - 1; i++) {        
        waypts.push({
            location:m_listString[i],
            stopover:true
        });        
    }
    var end = m_listString[m_listString.length - 1];
    
    var request = {
        origin: start, 
        destination: end,
        waypoints: waypts,
        optimizeWaypoints: true,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };
    directionsService.route(request, function(response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(response);            
        }else{
        	alert('Không tìm thấy đường đi');
        	alert(start);
        	
        }
        
    });
}